package com.wiz.weixin.manager;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import org.json.JSONException;
import org.springframework.stereotype.Service;

import com.wiz.weixin.VO.ActionInfo;
import com.wiz.weixin.VO.CodeTicket;
import com.wiz.weixin.common.GlobalConstant;
import com.wiz.weixin.common.TencentConstant;
import com.wiz.weixin.util.JSSDKUtil;
@Service("codeManager")
public class CodeManager implements TencentConstant{
	private final String OBTAIN_TICKET_URL="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN";
	public JSONObject getTicket(String actionName,String sceneStr,String sceneId) throws JSONException{
		String url = OBTAIN_TICKET_URL.replace("TOKEN", GlobalConstant.tencent_wx_token);
		
		Map<String,Object>scene = new HashMap<String, Object>();
		if(sceneId != null && !sceneId.equals("")){
			scene.put("scene_id", sceneId);
		}
		if(sceneStr != null && !sceneStr.equals("")){
			scene.put("scene_str", sceneStr);
		}
		ActionInfo actionInfo = new ActionInfo(scene);
		CodeTicket codeTicket = new CodeTicket( actionName, actionInfo);
		
		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(codeTicket);
		System.out.println(jsonObject.toString());
		String response = JSSDKUtil.httpsRequest(url, "POST", jsonObject.toString());
		System.out.println(response);
		org.json.JSONObject responseJson = new org.json.JSONObject(response);
		System.out.println(responseJson.toString());
		return responseJson;
		
	}
}
